package com.gomo;

import java.util.Calendar;
import org.ksoap2.serialization.SoapObject;
import com.gomo.entity.Cc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CcAddActivity extends Activity {
	public static Cc theObject;

	private EditText txtCcAddName;
	private EditText txtCcAddCardNumber;
	private EditText txtCcAddCvvCode;
	private TextView txtCcAddExpiryDate;
	private EditText txtCcAddPinNo;

	private Spinner spnrCcAddCardType;
	private Spinner spnrCcAddCardBank;
	AlertDialog alertDialog;

	private Button buttonCcAddCardExpiryDate;

	private int mYear;
	private int mMonth;
	private int mDay;
	static final int DATE_DIALOG_ID = 0;

	// private String _bankId = "";

	// String[] strings = { "CoderzHeaven", "Google", "Microsoft", "Apple",
	// "Yahoo", "Samsung" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cc_add);
		txtCcAddName = (EditText) findViewById(R.id.cc_add_text_name);
		txtCcAddCardNumber = (EditText) findViewById(R.id.cc_add_text_card_number);
		txtCcAddCvvCode = (EditText) findViewById(R.id.cc_add_text_card_cvv);
		txtCcAddExpiryDate = (TextView) findViewById(R.id.cc_add_text_card_expirty_dateDisplay);
		txtCcAddPinNo = (EditText) findViewById(R.id.cc_add_text_pin_number);

		spnrCcAddCardType = (Spinner) findViewById(R.id.cc_add_spinner_card_type);
		spnrCcAddCardBank = (Spinner) findViewById(R.id.cc_add_spinner_bank);

		buttonCcAddCardExpiryDate = (Button) findViewById(R.id.cc_add_button_card_expirty_dateDisplaypickDate);
		// get the current date
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		updateDisplay();

		/*
		 * spnrCcAddCardBank.setAdapter(new MyAdapter(CcAddActivity.this,
		 * R.layout.row, strings));
		 */
	}

	private void setUI() {
		txtCcAddName = (EditText) findViewById(R.id.cc_add_text_name);
		txtCcAddCardNumber = (EditText) findViewById(R.id.cc_add_text_card_number);
		txtCcAddCvvCode = (EditText) findViewById(R.id.cc_add_text_card_cvv);
		txtCcAddExpiryDate = (EditText) findViewById(R.id.cc_add_text_card_expirty_dateDisplay);
		txtCcAddPinNo = (EditText) findViewById(R.id.cc_add_text_pin_number);

		spnrCcAddCardBank = (Spinner) findViewById(R.id.cc_add_spinner_bank);
		spnrCcAddCardType = (Spinner) findViewById(R.id.cc_add_spinner_card_type);

		// btnSingnupSubmit = (Button) findViewById(R.id.signup_button_submit);
		createDialogMessage();

		/*
		 * spnrCcAddCardBank.setOnItemSelectedListener(new
		 * AdapterView.OnItemSelectedListener() { public void
		 * onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		 * Object item = parent.getItemAtPosition(pos); _bankId =
		 * item.toString(); } public void onNothingSelected(AdapterView<?>
		 * parent) { } });
		 */

	}

	public void buttonCcAddDatePickClick(View view) {
		showDialog(DATE_DIALOG_ID);
	}

	public void buttonCcAddSubmitClick(View view) {

		String selectedValBankId = getResources().getStringArray(
				R.array.bank_values)[spnrCcAddCardBank
				.getSelectedItemPosition()];

		GlobalService.MessageBox(selectedValBankId, this);

		/*
		 * if (isValidate()) { new ParseXmlTaskAddCc().execute(); }
		 */
	}

	private boolean isValidate() {
		if (!GlobalService.isOnline(this)) {
			GlobalService.MessageBox("No or limited internet connection", this);
			return false;
		}
		return true;
	}

	private void updateDisplay() {
		buttonCcAddCardExpiryDate.setText(new StringBuilder()
				.append(mMonth + 1).append("-").append(mYear).append(" "));
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		}
		return null;
	}

	private void createDialogMessage() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("").setCancelable(false)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		alertDialog = builder.create();
	}

	private String processServiceAddCc() {
		SoapService ss = new SoapService();

		String soap_method_name = getString(R.string.method_cc_add);
		String soap_namespace = getString(R.string.service_namespace);
		String soap_url = getString(R.string.service_url);
		String soap_action = soap_namespace + soap_method_name;
		String android_id = Secure.getString(getBaseContext()
				.getContentResolver(), Secure.ANDROID_ID);

		SoapObject soap_object_request = new SoapObject(soap_namespace,
				soap_method_name);

		soap_object_request.addProperty("UserId", GlobalService.getUserId());

		soap_object_request.addProperty("Name", txtCcAddName.getText()
				.toString());
		soap_object_request.addProperty("CardType", spnrCcAddCardType
				.getSelectedItem().toString());

		soap_object_request.addProperty("CardNumber", txtCcAddCardNumber
				.getText().toString());
		soap_object_request.addProperty("CVVCode", txtCcAddCvvCode.getText()
				.toString());

		soap_object_request.addProperty("ExpiryDate", buttonCcAddCardExpiryDate
				.getText().toString());

		String selectedValBankId = getResources().getStringArray(
				R.array.bank_values)[spnrCcAddCardBank
				.getSelectedItemPosition()];

		soap_object_request.addProperty("BankId", selectedValBankId);

		soap_object_request.addProperty("PinNo", txtCcAddPinNo.getText()
				.toString());

		soap_object_request.addProperty("PinCode",
				GlobalService.getUserPinCode());

		soap_object_request.addProperty("DeviceId", android_id);
		soap_object_request.addProperty("DeviceType", "2");

		SoapObject soap_object_response = ss.parseWebService(soap_action,
				soap_url, soap_object_request);
		SoapObject recordsArray = (SoapObject) soap_object_response
				.getProperty(0);
		String soapresultid = recordsArray.getProperty("ID").toString();
		String soapresultmsg = recordsArray.getProperty("Message").toString();
		return soapresultid + "|" + soapresultmsg;
	}

	class ParseXmlTaskAddCc extends AsyncTask<Void, Void, String> {
		private final ProgressDialog dialog = new ProgressDialog(
				CcAddActivity.this);

		protected void onPreExecute() {
			this.dialog.setMessage("Adding credit card...");
			this.dialog.show();
		}

		protected String doInBackground(Void... unused) {
			return processServiceAddCc();
		}

		protected void onPostExecute(String soapresultmsg) {
			if (this.dialog.isShowing())
				this.dialog.dismiss();

			String[] arrString = GlobalService.splitString(soapresultmsg, "|");
			String id = arrString[0];
			String message = arrString[1];

			alertDialog.setMessage(message);
			alertDialog.show();

			if (!id.equalsIgnoreCase("0")) {
				finish();
			}
		}
	}

}